package com.woniuxy.owner.controller;

import com.woniuxy.owner.pojo.Owner;
import com.woniuxy.owner.service.OwnerService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@CrossOrigin
public class OwnerController {
    private OwnerService ownerService;

    @RequestMapping("/queryOwner")
    public List<Owner> queryOwner() {
        return ownerService.list();
    }

    @PostMapping("/addOwner")
    @ResponseBody
    public void addOwner(@RequestBody Owner owner) {
        ownerService.addOwner(owner);
    }

    // 根据id查询业主
    @GetMapping("/queryOwnerById/{id}")
    public Owner queryOwnerById(@PathVariable("id") Long id) {
        return ownerService.getById(id);
    }

    // 根据楼栋id查询业主
    @GetMapping("/queryOwnerByBuildingId/{buildingId}")
    public List<Owner> queryOwnerByBuildingId(@PathVariable Long buildingId) {
        return ownerService.list().stream()
                .filter(owner -> buildingId.equals(owner.getBuildingId()))
                .collect(Collectors.toList());
    }

    // 小程序端业主登录
    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password) {
        return ownerService.login(username, password);
    }
    //小程序端房主房屋信息
    @GetMapping("/house")
    public Owner house(){
        return ownerService.house();
    }
}
